/******************** 
	作用:商品列表
	作者:蔡俊雄
	版本:V1.0
	时间:2015-08-05
********************/

$(function() {
	var tableId = "#dynamic-table"; //表格ID
	var upShelfUrl = "/shop/admin/product/shelfStoreProduct?status=1"; //上架的网址
	var offShelfUrl = "/shop/admin/product/shelfStoreProduct?status=2"; //下架的网址
	var deleteUrl = "/shop/admin/product/storeProductList/delete"; //删除的地址



	General.initCheckbox(); //初始化表格中的复选框事件监听
	General.extendDialog(); //扩展对话框

	//-----------------上架-----------------
	//点击"上架"按钮
	$("#onShelf,#onShelf2").on("click", function(e) {
		General.stopEvent(e);
		//判断是否有行被选中
		var ids=$("#checkedIds").val();
		if (ids.length > 0) {
			startOnShelf(ids);
		} else {
			General.alert("请先选中要上架的商品再进行操作!");
		}
	});
	//开始上架
	function startOnShelf(ids) {
		General.showMask();
		if(typeof(ids)=="object"){
			ids=ids.join(",");
		}
		var url = upShelfUrl;
		var data = {
			"goods_id": ids,
		};
		var tip = "正在上架";
		Common.ajaxPost(null, null, url, data, tip, completeOnShelf, failOnShelf);
	};
	//上架成功
	function completeOnShelf(options, data, status) {
		data=$.parseJSON(data);		
		
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){
			
			alert(data.sub_msg);
			General.hideMask(); 
			
		}
	};
	//上架失败
	function failOnShelf() {
		General.hideMask();
		General.alert("上架失败!");
	};
	//-----------------下架-----------------
	//点击"下架"按钮
	$("#offShelf,#offShelf2").on("click", function(e) {
		General.stopEvent(e);
		//判断是否有行被选中
		var ids=$("#checkedIds").val();
		if (ids.length > 0) {
			startOffShelf(ids);
		} else {
			General.alert("请先选中要下架的商品再进行操作!");
		}
	});
	//开始下架
	function startOffShelf(ids) {
		General.showMask();
		if(typeof(ids)=="object"){
			ids=ids.join(",");
		}
		var url = offShelfUrl;
		var data = {
			"goods_id": ids,
		};
		var tip = "正在下架";
		Common.ajaxPost(null, null, url, data, tip, completeOffShelf, failOffShelf);
	};
	//下架成功
	function completeOffShelf(options, data, status) {
		data=$.parseJSON(data);		
		
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){
			
			alert(data.sub_msg);
			General.hideMask(); 
			
		}
	};
	//下架失败
	function failOffShelf() {
		General.hideMask();
		General.alert("下架失败!");
	};
	//-----------------删除-----------------
	//点击删除所选按钮
	$("#delete,#delete2").on("click", function(e) {
		General.stopEvent(e);
		//判断是否有行被选中
		var ids=$("#checkedIds").val();
		if (ids.length > 0) {
			//弹出确认框
			General.confirm("您确定要删除所有选中的商品吗?",null,startDelete,null,ids);
		} else {
			General.alert("请先选中要删除的商品再进行操作!");
		}
	});
	//点击行内"删除"按钮
	$(tableId + ">tbody a[data-id=delete]:not([disabled])").on("click", function(e) {
		var tr = $(this).closest("tr");
		var goods_id = tr.attr("data-id");
		General.confirm("您确定要删除该商品吗?",null,startDelete,null,goods_id);
	});
	//开始删除
	function startDelete(ids) {
		General.showMask();
		if(typeof(ids)=="object"){
			ids=ids.join(",");
		}
		var options = {
			"goods_id": ids
		};
		var url = deleteUrl;
		var data = {
			"goods_id": ids
		};
		var tip = "正在删除";
		Common.ajaxPost(options, null, url, data, tip, completeDelete, failDelete);
	};
	//删除成功
	function completeDelete(options, data, status) {
		
	data=$.parseJSON(data);		
		
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){
			
			alert(data.sub_msg);
			General.hideMask(); 
			
		}
	};
	//删除失败
	function failDelete() {
		General.hideMask();
		General.alert("删除失败!");
	};
	//用于挂件的选择商品
	 $("a[data-id='forWidgetSelectPro']").click(function(){
	 	var THIS=$(this);
	 	//alert('选择商品：'+THIS.attr('data-sent'));
	 	window.parent.General.closeDialog();
	 	window.parent.forAautoRenderWebCompent_selectProImg(THIS.attr('data-sent'));
	 });
	
});